<?php /*a:1:{s:64:"F:\mhz\2019\thinkphp5queue\application\index\view\buy\order.html";i:1530005905;}*/ ?>
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/static/js/jquery.min.js"></script>
</head>
<body>
    <h2><?php echo htmlentities($user['name']); ?>等待秒杀结果,请勿关闭页面！！</h2>
    <!--实现长轮询请求服务器秒杀结果的接口-->

    <script type="text/javascript">
            $(function () {
                (function longPolling() {
//                    alert(Date.parse(new Date())/1000);
                    $.ajax({
                        url: '/index/buy/orderResult',
                        type : 'POST',
                        data: {
                             user: "<?php echo htmlentities($user['name']); ?>",
                             count : "5" ,
                            },
                        dataType: "json",
                        timeout: 10000,//5秒超时，可自定义设置
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(textStatus);
                          //  $("#state").append("[state: " + textStatus + ", error: " + errorThrown + " ]<br/>");
                            if (textStatus == "timeout") { // 请求超时
                                longPolling(); // 递归调用
                            } else { // 其他错误，如网络错误等
                                longPolling();
                            }
                        },
                        success: function (data, textStatus) {
                            console.log(data);
                           // $("#state").append("[state: " + textStatus + ", data: { " + data + "} ]<br/>");
                            if (data.code == "10001") { // 请求成功
                                //longPolling();
                                console.log('恭喜你秒杀成功!');
                            }else if(data.code == 20001){
                                switch (data.status){
                                    case "1":
                                        console.log('队列已满，请稍后重试!');
                                        break;
                                    case "2":
                                        console.log('很遗憾，秒杀失败，库存空空如也!');
                                        break;
                                    case "3":
                                        console.log('您已成功秒杀过，不可以贪心了!');
                                        break;
                                    default:
                                        console.log('秒杀已经结束');
                                }
                                //window.history.go(-1);
                            }
                        }
                    });

                })();
            });
</script>

</body>
</html>



